#pragma once

#include <iostream>
#include <string>
#include <memory>
#include <functional>
#include "InetAddr.hpp"

class Rector;

// 描述一个连接
class Connection
{
public:
    Connection() : _sockfd(-1), _events(0)
    {
    }

    void SetPeerInfo(const InetAddr &peer_addr)
    {
        _peer_addr = peer_addr;
    }

    void SetSockfd(int sockfd)
    {
        _sockfd = sockfd;
    }

    int Sockfd() { return _sockfd; }

    void SetEvents(uint32_t events)
    {
        _events = events;
    }

    uint32_t GetEvents() { return _events; }

    void SetOwner(Rector *owner)
    {
        _owner = owner;
    }
    Rector *GetOwner()
    {
        return _owner;
    }

    ~Connection()
    {
    }

    void Append(const std::string &in)
    {
        _inbuffer += in; // 追加到输入缓冲区
    }
    void AppendToOut(const std::string &out)
    {
        _outbuffer += out;
    }
    void DisCardOutString(int n)
    {
        _outbuffer.erase(0, n);
    }
    bool IsOutBufferEmpty() { return _outbuffer.empty(); }
    std::string &InBuffer()
    {
        return _inbuffer;
    }
    std::string &OutString()
    {
        return _outbuffer;
    }
    void Close()
    {
        if (_sockfd >= 0)
            close(_sockfd);
    }
    virtual void Sender() = 0;
    virtual void Recever() = 0;
    virtual void Excepter() = 0;

protected:
    int _sockfd;
    std::string _inbuffer;
    std::string _outbuffer;

    InetAddr _peer_addr;

    Rector *_owner;
    uint32_t _events; // 该连接关心的事件
};